]> git.r.bdr.sh - rbdr/map/blame - Map/Presentation/Base Components/SearchBar.swift
Add some debouncing
[rbdr/map] / Map / Presentation / Base Components / SearchBar.swift
CommitLineData
be897af3 1// Copyright (C) 2024 Rubén Beltrán del Río
14491563 2
be897af3
RBR
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
14491563 7
be897af3
RBR
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
14491563 12
be897af3
RBR
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see https://map.tranquil.systems.
14491563
RBR
15import SwiftUI
16
17struct SearchBar: View {
18
19 @Binding var term: String
20 @FocusState var isSearchFocused: Bool
21 var onNext: () -> Void = {}
22 var onPrevious: () -> Void = {}
23 var onSubmit: () -> Void = {}
24 var onDismiss: () -> Void = {}
25
26 var body: some View {
27 HStack(spacing: 2.0) {
28 ZStack {
29 TextField("Search", text: $term)
30 .textFieldStyle(.roundedBorder)
31 .padding(.trailing, 16.0)
32 .focused($isSearchFocused)
33 .onAppear {
34 isSearchFocused = true
35 }
36 .onKeyPress { press in
37 if press.key == .return {
38 if press.modifiers.contains(.shift) {
39 onPrevious()
40 return .handled
41 }
42 onNext()
43 return .handled
44 }
45 return .ignored
46 }
47 }
48 Spacer()
49 Button(action: onPrevious) {
50 Image(systemName: "chevron.left")
be897af3 51 .font(.Theme.smallControl)
14491563
RBR
52 }.keyboardShortcut(
53 "g", modifiers: EventModifiers([.command, .shift])
54 ).help("Find Previous (⇧⌘G)")
55 Button(action: onNext) {
56 Image(systemName: "chevron.right")
be897af3 57 .font(.Theme.smallControl)
14491563
RBR
58 }.keyboardShortcut(
59 "g", modifiers: EventModifiers([.command])
60 ).help("Find Next (⌘G)")
61 Button(action: onDismiss) {
62 Text("Done")
be897af3 63 .font(.Theme.smallControl)
14491563
RBR
64 }.keyboardShortcut(.escape, modifiers: EventModifiers())
65 .help("Done (⎋)")
66 }.padding(4.0)
67 }
68}
69
70#Preview {
71 SearchBar(term: .constant("Hello"))
72}